import Vue from 'vue'
import Router from 'vue-router'
import storage from '@/storage'
import keys from '@/storage/keys'
Vue.use(Router)

const router = new Router({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import( './views/auth/login.vue'),
      meta: {title: '登录', loginAuth: false}
    },
    {
      path: '/',
      name: 'home',
      component: () => import( './views/home.vue'),
      meta: {title: '首页', loginAuth: true},
      redirect: '/customer',
      children: [
        {
          path: '/customer',
          name: 'customer',
          component: () => import( './views/customer/customer.vue'),
          meta: {title: '客户管理', loginAuth: true}
        },
        {
          path: '/bill',
          name: 'bill',
          component: () => import( './views/customer/bill.vue'),
          meta: {title: '客户账单管理', loginAuth: true}
        },
        {
          path: '/content',
          name: 'content',
          component: () => import( './views/content/content.vue'),
          meta: {title: '服务管理', loginAuth: true}
        },
        {
          path: '/addConsult',
          name: 'addConsult',
          component: () => import( './views/content/addConsult.vue'),
          meta: {title: '新增服务内容', loginAuth: true}
        },
        {
          path: '/banner',
          name: 'banner',
          component: () => import( './views/content/banner.vue'),
          meta: {title: 'banner管理', loginAuth: true}
        },
        {
          path: '/addBanner',
          name: 'addBanner',
          component: () => import( './views/content/addBanner.vue'),
          meta: {title: '新增banner', loginAuth: true}
        },
        {
          path: '/partner',
          name: 'partner',
          component: () => import( './views/partners/partner.vue'),
          meta: {title: '合作伙伴管理', loginAuth: true}
        },
        {
          path: '/addpartner',
          name: 'addpartner',
          component: () => import( './views/partners/addpartner.vue'),
          meta: {title: '新增合作伙伴', loginAuth: true}
        },
        {
          path: '/business',
          name: 'business',
          component: () => import( './views/business/business.vue'),
          meta: {title: '商学院', loginAuth: true}
        },
        {
          path: '/business_add',
          name: 'business_add',
          component: () => import( './views/business/add.vue'),
          meta: {title: '新增内容', loginAuth: true}
        },
        {
          path: '/problem',
          name: 'problem',
          component: () => import( './views/problemPar/problem.vue'),
          meta: {title: '问题管理', loginAuth: true}
        },
        {
          path: '/news',
          name: 'news',
          component: () => import( './views/news/index.vue'),
          meta: {title: '新闻中心', loginAuth: true}
        },
        {
          path: '/news_add',
          name: 'news_add',
          component: () => import( './views/news/add.vue'),
          meta: {title: '新增新闻信息', loginAuth: true}
        },
        {
          path: '/column',
          name: 'column',
          component: () => import( './views/column/column.vue'),
          meta: {title: '栏目管理', loginAuth: true}
        },
        {
          path: '/contact',
          name: 'contact',
          component: () => import( './views/contact/contact.vue'),
          meta: {title: '联系我们', loginAuth: true}
        },
      ]
    },
    {
      path: '/logout',
      name: 'logout',
      component: () => import( './views/auth/logout.vue'),
      meta: {title: '退出登录', loginAuth: false}
    },
  ],
  // 页面切换后 使页面正常显示
  scrollBehavior() {
    return {x: 0, y: 0}
  }
})
/**
 * 路由权限控制
 */
router.beforeEach((to, from, next) => {
  //如果未匹配到路由
  if (to.matched.length === 0) {
    from.name ? next({name: from.name}) : next('/404')
  }
  //设置页面标题
  document.title = to.meta.title || ''
  //前往登录页若已经登录直接跳转首页
  if (to.path === '/login') {
    if (storage.isLogin === 'true') {
      next({path: '/'})
    }
  } else {
    //判断路由是否需要登录验证
    if (Object.prototype.hasOwnProperty.call(to.meta, "loginAuth") && to.meta.loginAuth === true) {
      storage.getItem(keys.isLogin).then(data => {
        if (data !== true) {
          next({path: '/logout'})
          return
        } else {
          next()
        }
      })
    }
  }
  next()
})
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router